package ru.reaperoq.inventory.components.lists

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.ListItem
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import my.models.ComponentUi

@Composable
fun ComponentsList(
    modifier: Modifier = Modifier,
    components: List<ComponentUi>,
    onClick: ((Int) -> Unit)? = null
) {
    LazyColumn(
        modifier = modifier,
        contentPadding = PaddingValues(8.dp),
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.spacedBy(8.dp)
    ) {
        if (components.isEmpty()) {
            item {
                Text(text = "Нет компонентов")
            }
        }
        items(components) { component ->
            ListItem(
                modifier = Modifier
                    .then(
                        if (onClick != null) Modifier.clickable { onClick(component.id) }
                        else Modifier
                    ),
                headlineContent = { Text(text = "${component.type} ${component.name}") },
                supportingContent = {
                    Column {
                        Text(text = component.serialNumber)
                        Text(text = component.purchaseDate)
                        Text(text = "Добавил: ${component.createdBy.username}")
                        Text(text = "Подтвердил: ${component.verifiedBy.username}")
                        Text(text = "Последнее подтверждение:\n${component.lastVerifiedAt}")
                        Text(text = component.location.name)
                    }
                }
            )
        }
    }
}